<?php

class bannerModel extends Model {

	const INDEX = 1; //首页顶部图片
	const GOODS_INDEX = 2; //商城首页轮播图
	const BANNER_CATEGORY_LIST = 'BANNER_LIST_';

	protected $_primaryKey = 'banner_id';

	public function jumpTypeList() {
		return [
			'none' => '无动作',
			'webview' => 'URL跳转',
			'goodsInfo' => '商品详情跳转',
			'dynamicInfo' => '动态详情跳转',
			'shopId' => '品牌跳转',
			'activityInfo' => '活动标签跳转',
		];
	}

	public function updateById($id, $data) {
		if ($this->update($data, $this->_primaryKey . '=?', $id)) {
			$list_id = isset($data['parent_id']) && $data['parent_id'] ? $data['parent_id'] : $id;
			Cache_Memcache::getInstance()->delete(self::BANNER_CATEGORY_LIST . $list_id);
			return true;
		}
		return false;
	}

	public function findById($id) {
		return $this->getOne('banner_id=?', $id);
	}

	public function getBannerList($banner_id) {
		$list = Cache_Memcache::getInstance()->get(self::BANNER_CATEGORY_LIST . $banner_id);
		if ($list !== false) {
			return $list;
		}
		$list = [];
		$listData = $this->fields(['banner_id,name,image,jump_type,jump_url,jump_id'])->order('sort asc,banner_id desc')->getAll('parent_id=?', [$banner_id]);
		if ($listData) {
			foreach ($listData as $bannerInfo) {
				$list[] = [
					'bannerId' => intval($bannerInfo['banner_id']),
					'name' => strval($bannerInfo['name']),
					'image' => strval($bannerInfo['image']),
					'jumpType' => strval($bannerInfo['jump_type']),
					'jumpUrl' => strval($bannerInfo['jump_url']),
					'jumpId' => intval($bannerInfo['jump_id']),
				];
			}
		}
		Cache_Memcache::getInstance()->set(self::BANNER_CATEGORY_LIST . $banner_id, $list, 60);
		return $list;
	}

	public function getSingleBanner($banner_id) {
		if (!$bannerInfo = $this->getOne('parent_id=?', $banner_id)) {
			return (object) [];
		}
		return [
			'bannerId' => intval($bannerInfo['banner_id']),
			'name' => strval($bannerInfo['name']),
			'image' => strval($bannerInfo['image']),
			'jumpType' => strval($bannerInfo['jump_type']),
			'jumpUrl' => strval($bannerInfo['jump_url']),
			'jumpId' => intval($bannerInfo['jump_id']),
		];
	}

}
